package edu.gmu.middleware;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class MessageRegistry
{
    /**
     * List of MessageBindings
     */
    private Map messageBindings = new LinkedHashMap();
    
    /**
     * Creates a new MessageRegistry with specified values.
     */
    public MessageRegistry( )
    {
        
    }


    /**
     * Gets messageBinding from given id
     * @param id of messageBinding to get
     * @return messageBinding of given id or null if not found
     */
    public MessageBinding getMessageBinding( int id )
    {
        Integer key = new Integer(id);
        MessageBinding resolver = (MessageBinding)this.messageBindings.get(key);
        return resolver;
    }

    /**
     * Gets messageBinding from given name (first one found - maybe more than one mapping???)
     * @param name of messageBinding to get
     * @return enum of given messageBinding or null if not found
     */
    public MessageBinding getMessageBinding( Class soughtClass )
    {
        Integer key = null;
        MessageBinding resolver = null;
        
        Set keys = this.messageBindings.keySet();
        Iterator i = keys.iterator();
        while( i.hasNext() == true )
        {
            Integer aKey = (Integer)i.next();
            MessageBinding aResolver = (MessageBinding)this.messageBindings.get(aKey);
            Class aClass = aResolver.getBoundClass();
            if( soughtClass.equals(aClass) )
            {
                if( resolver != null )
                {
                    String s = "Duplicate registrations for class " +
                            soughtClass.getName() + " keys " + key + " and "
                            + aKey;
                    throw new IllegalStateException(s);
                }
                key      = aKey;
                resolver = aResolver;
            }
        }
        return resolver;
    }
    
    /**
     * Registers binding between given id and class
     * @param id
     * @param class to bind to id
     * @returns the newly registered MessageRegistry
     * @throws IllegalArgumentException if id is already bound
     */
    public MessageBinding register( int id, Class aClass )
    {
        MessageBinding resolver = getMessageBinding(id);
        if( resolver != null )
        {
            String s = "Id is already bound: <" + id + ", "
                    + resolver.getBoundClass().getName()+">";
            throw new IllegalArgumentException(s);
        }
        resolver = new MessageBinding( id, aClass );
        this.messageBindings.put(new Integer(id), resolver);
        
        return resolver;
    }
    
    /**
     * Unregisters binding between given id and class
     * @param id to unregister
     * @returns true if unbound, false if no binding exists
     */
    public boolean unregister( int id )
    {
        Integer key = new Integer(id);
        if( this.messageBindings.containsKey(key) == false )
        {
            return false;
        }
        
        this.messageBindings.remove(key);
        
        return true;
    }


    /**
     * Gets this String facsimile of this object.
     * @return name
     */
    public String toString( )
    {
        StringBuffer buf = new StringBuffer();
        buf.append( "MessageRegistry: size=" + this.messageBindings.size() );
        for (int i = 0; i < this.messageBindings.size(); i++)
        {
            MessageBinding messageBinding = (MessageBinding)this.messageBindings.get(i);
            buf.append( "    "+messageBinding.toString() );
        }
        return buf.toString();
    }
}